﻿Imports System.ComponentModel.DataAnnotations

Public Class BaseVO

    Public Function validate(Optional throwException As Boolean = True) As List(Of ValidationResult)
        Dim validationResults As New List(Of ValidationResult)
        Dim vc As New ValidationContext(Me, Nothing, Nothing)
        Dim isValid As Boolean = Validator.TryValidateObject(Me, vc, validationResults, True)

        If Not isValid AndAlso throwException Then

            Dim msg As String = ""
            For Each vr In validationResults
                If msg <> "" Then
                    msg &= " " & vbCrLf
                End If
                msg &= vr.ErrorMessage
            Next

            Throw New Exception(msg)
        End If

        Return validationResults
    End Function

End Class